package org.he.auth.controller;

import org.he.auth.service.IPermissionService;
import org.he.auth.domain.Permission;
import org.he.auth.query.PermissionQuery;
import org.he.basic.util.AjaxResult;
import org.he.basic.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;


    /**
     * 保存和修改公用的
     * @param permission 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Permission permission) {
        try {
            if ( permission.getId() != null)
                permissionService.update(permission);
            else
                permissionService.insert(permission);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
                permissionService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Permission permission =permissionService.queryById(id);
            return AjaxResult.success(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }
    // 查询所有权限
    @GetMapping("/tree")
    public AjaxResult getPermissionTree() {
        try {
            List<Permission> tree = permissionService.getPermissionTree();
            return AjaxResult.success(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取权限树失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll() {

        try {
            List< Permission> list = permissionService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody PermissionQuery query) {
        try {
            PageInfo<Permission> page = permissionService.queryPage(query);
            return AjaxResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            permissionService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }
}
